package com.apes.framework.plugin.fsm;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.express.FuncAlias;
import com.apes.framework.plugin.fsm.core.FsmConfig;
import com.apes.framework.plugin.outbox.annotation.OutboxSubscriber;
import com.apes.framework.plugin.soe.metadata.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service("fsmStateService")
public class FsmStateService extends DomainService {

    @Autowired
    private FsmStateRepository fsmStateRepository;
    @Autowired
    private FsmStateInstanceRepository fsmStateInstanceRepository;

    /***
     * 功能：保存
     * @param request
     * @return
     */
    public FsmState sava(SimpleRequest request) {
        FsmState fsmState = request.getO(FsmState.class);
        fsmState.verify();
        return fsmStateRepository.saveAndFlush(fsmState);
    }

    /**
     * 功能：修改
     *
     * @param request
     * @return
     */
    public FsmState update(SimpleRequest request) {
        String id = request.get("id");
        FsmState fsmState = request.getO(FsmState.class);
        fsmState.verify();
        FsmState tempFsmState = fsmStateRepository.findOne(id);
        if (tempFsmState == null) throw new RuntimeException("状态：" + id + " 在系统中不存在。");
        if (tempFsmState.getVersion() != fsmState.getVersion())
            throw new RuntimeException("记录已更新或已删除，请重新提交！");  // 用于判断当前版本是否被修改
        return fsmStateRepository.saveAndFlush(fsmState);
    }

    /***
     * 功能：删除
     * @param request
     * @return
     */
    public String delete(SimpleRequest request) {
        String id = request.get("id");
        FsmState fsmState = request.getO(FsmState.class);
        FsmState tempFsmState = fsmStateRepository.findOne(id);
        if (tempFsmState == null) throw new RuntimeException("状态：" + id + "在系统中不存在。");
        if (tempFsmState.getVersion() != fsmState.getVersion()) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        fsmStateRepository.delete(fsmState);
        return "删除成功";
    }

    @OutboxSubscriber(name = "状态流：增加")
    public void subscribeSaveEvent(FsmState fsmState) {
        SpringManager.getBean(StringRedisTemplate.class).convertAndSend(FsmConfig.FSM_SETTING_TOPIC, fsmState.getFsmFlow().getId());
    }

    @OutboxSubscriber(name = "状态流：删除", eventType = "D")
    public void subscribeDeleteEvent(FsmState fsmState) {
        SpringManager.getBean(StringRedisTemplate.class).convertAndSend(FsmConfig.FSM_SETTING_TOPIC, fsmState.getFsmFlow().getId());
    }

    @FuncAlias(name = "获取审批意见")
    public String findRemarkByInstance(String instanceId,String operate){
        FsmStateInstance fsmStateInstance = fsmStateInstanceRepository.findOne(instanceId);
        String remark = "";
        try {
             remark = fsmStateRepository.findRemark(fsmStateInstance.getFsmFlowInstance().getBusinessType().getId(), fsmStateInstance.getFsmFlowInstance().getBusinessKey(), operate);
        }catch (NullPointerException e){
            return remark;
        }
        return remark;
    }

}
